<template>
  <div class="main">
    <div class="main_header">
      <div class="main_header_div">
        <div class="main_header_div_left">
          <a href=""><img src="../../img/logo.png" height="78" width="178"></a>
        </div>
        <div class="main_header_div_right">
          <span>| 找回密码</span>
        </div>
      </div>
    </div>
    <div>
      <div class="kong"></div>
      <div class="little_part">
        <span style="font-size: 18px">手&nbsp;&nbsp;机&nbsp;&nbsp;号</span>
        <input id="id" type="text" class="kuang" placeholder="请输入手机号" v-model="telephone" @change="check_telephone"/>
        <span class="right" id="error_id" style="color: red" v-model="err_telephone">{{err_telephone}}</span>
      </div>
      <div class="little_part">
        <span style="font-size: 18px">密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码</span>
        <input id="pwd" type="password" class="kuang" placeholder="请输入密码" v-model="password" @change="check_password"/>
        <span class="right" style="color: red" id="error_pwd" v-model="err_password">{{err_password}}</span>
      </div>
      <div class="little_part">
        <span style="font-size: 18px">确认密码</span>
        <input id="name" type="password" class="kuang" placeholder="请再次输入密码" @change="check_twopassword" v-model="twopassword"/>
        <span class="right" style="color: red" id="error_twopassword" v-model="err_twopassword">{{err_twopassword}}</span>
      </div>
      <div class="little_part">
        <span style="font-size: 18px">验&nbsp;&nbsp;证&nbsp;&nbsp;码</span>
        <input id="yanzheng" type="text" class="kuang01" placeholder="请输入验证码" @change="check_checking" v-model="checking_code"/>
        <button id="yan" @click="fasong" v-model="gain_text" :disabled="gain_text==='获取验证码'?false:true">{{gain_text}}
        </button>
        <span class="right" style="color: red" id="error_yanzheng"
              v-model="err_checking_code">{{err_checking_code}}</span>
      </div>
      <div class="finish"><input id="btn_register" type="button" value="找回密码" @click="retrieve_go"></div>

    </div>
  </div>

</template>

<script>
  export default {
    name: "retrieve",
    data() {
      return {
        telephone: '',
        err_telephone: '',
        password: '',
        err_password: '',
        checking_code: '',
        err_checking_code: '',
        twopassword: '',
        err_twopassword: '',
        gain_text: '获取验证码',
        code_dist: {},
      }
    },

    methods: {
      close: function () {
        Bus.$emit('close', false)
      },
      login: function () {
        Bus.$emit('resign', false);
        Bus.$emit('login', true)
      },
      open1: function () {
        this.$message({
          showClose: true,
          message: '恭喜您修改密码成功',
          type: 'success'
        });
      },
      // open2() {
      //   this.$message({
      //     showClose: true,
      //     message: '此手机号未注册，请您先注册！',
      //     type: 'warning'
      //   });
      // },
      check_telephone: function () {
        var reg_tel = /^1[345678]\d{9}$/;
        if (this.telephone) {
          if (reg_tel.test(this.telephone)) {
            this.err_telephone = "";
            return true
          } else {
            this.err_telephone = "* 请输入正确的手机号";
            return false;
          }
        } else {
          this.err_telephone = "* 请输入手机号";
          return false;
        }
      },
      check_password: function () {
        var reg_pwd = /^\w{6,18}$/;
        if (this.password) {
          if (reg_pwd.test(this.password)) {
            this.err_password = "";
            return true;
          } else {
            this.err_password = "* 密码格式不合法";
            return false;
          }
        } else {
          this.err_password = "* 请输入密码";
          return false;
        }
      },
      check_checking: function () {
        var reg_checking = /^\d{4}$/;

        if (this.checking_code) {
          if (reg_checking.test(this.checking_code)) {
            this.err_checking_code = "";
            return true;
          } else {
            this.err_checking_code = "* 请输入正确的验证码";
            return false;
          }
        } else {
          this.err_checking_code = "* 请输入验证码";
          return false;
        }
      },
      check_twopassword: function () {
        if (this.twopassword) {
          if (this.password==this.twopassword) {
            this.err_twopassword = "";
            return true;
          } else {
            this.err_twopassword = "* 两次输入密码不一致";
            return false;
          }
        } else {
          this.err_twopassword = "* 请再次输入密码";
          return false;
        }
      },
      //修改发送验证码
      fasong: function () {
        let this_new01 = this;
        if (this.check_telephone()) {
          this.$axios.post('http://127.0.0.1:8080/user/code/', {
            "telephone": this.telephone,
          })
            .then(function (response) {
              console.log(response.data);
              if (response.data.code === 0) {
                this_new01.code_dist[response.data.tel] = response.data.vf_code
                var seconds = 6;
                this_new01.gain_text = seconds + '秒后继续发送';
                var inter = setInterval(function () {
                  seconds -= 1;
                  this_new01.gain_text = seconds + '秒后继续发送';
                  if (seconds <= 0) {
                    this_new01.gain_text = '获取验证码';
                    clearInterval(inter);
                  }
                }, 1000)
              }
            })
            .catch(function (error) {
              console.log(error);
            });
        }
      },
      // 修改密码
      retrieve_go: function () {
        let this_new02 = this;
        if (this.check_telephone() && this.check_checking() && this.check_twopassword() && this.check_password()) {
          if (this.password == this.twopassword) {
            if (this.code_dist[this.telephone] && this.checking_code === this.code_dist[this.telephone]) {
              this.$axios.post('http://127.0.0.1:8080/user/retrieve/', {
                "telephone": this_new02.telephone,
              })

                .then( (response)=> {

                  if (response.data.code == '1') {
                    this.$axios.post('http://127.0.0.1:8080/user/modify_a/', {
                      "telephone": this_new02.telephone,
                      "new_password": this_new02.password,
                    })
                      .then( (result)=>{
                        if(result) {
                          if (result.data.code === '1003') {
                            console.log("sss");
                            this.$router.push("/login");
                            this_new02.open1();
                          }
                        }
                      })

                  }
                })
                .catch(function (error) {
                  console.log(error);
                });
            } else {
              this.err_checking_code = '* 验证码错误'
            }
          }else{
            this.err_twopassword = "* 两次输入密码不一致";
          }
        }
      },

    }
  }
</script>

<style scoped>
  .main {
    /*border: solid 2px black;*/
    width: 900px;
    height: 630px;
    margin: auto;
    margin-top: 40px;
    box-shadow: 0px 0px 10px 5px #aaa;


  }

  .header_ti {
    height: 50px;
    width: 1520px;
  }

  .main_header {
    width: 900px;
    height: 100px;
    display: flex;
    justify-content: center;
    background-color: #2b2b32;
    margin-bottom: 30px;
  }

  .main_header_div {
    display: flex;
    justify-content: center;
    padding-top: 10px;
  }

  .main_header .main_header_div span {
    padding-top: 20px;
    margin-bottom: 10px;
    margin-left: 20px;
    font-size: 30px;
    color: white;
    width: 72px;
  }

  .main_header_div img {
    height: 38px;
    width: 60px
  }

  .main_header_div_left {
    padding-top: 20px;
  }

  .main_header_div_right {
    padding-top: 20px;
  }

  .kong {
    height: 13px;
  }

  .little_part {
    margin-top: 10px;
    margin-left: 310px;
    /*width: 400px;*/
    height: 80px;
    line-height: 80px;
    /*text-align: center;*/

  }

  .kuang {
    width: 260px;
    height: 40px;
    margin-left: 10px;
  }

  .kuang01 {
    width: 130px;
    height: 40px;
    margin-left: 10px;

  }

  #yan {
    width: 120px;
    height: 40px;
    background: red;
    color: white;

  }


  .finish {
    margin-top: 40px;
    margin-left: 410px;
    height: 70px;
    width: 140px;
  }

  .finish input {
    height: 50px;
    width: 140px;
    background-color: red;
    border: none;
    font-size: 20px;
    color: white;
  }

  .finish a {
    color: #2b2b32;
    text-decoration: none;
  }

  .finish a:hover {
    color: #d63318;
    text-decoration: none;
  }
</style>
